import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from 'vue-router'
import stuteRoutes from './stuteRouter'
import { useUserStore } from "@/store/userState";

const routes: Array<RouteRecordRaw> = [
    {
        name: 'layout',
        path: '/layout',
        component: () => import('@/layout/index.vue')
    },
    {
        name: 'Login',
        path: '/login',
        component: () => import('@/views/Login/index.vue')
    },
    ...stuteRoutes
]
const router = createRouter({
    history: createWebHistory(),
    routes,
    strict: true
})

router.beforeEach((to) => {
    const userStore = useUserStore()
    const isAuthenticated = userStore.isAuthenticated
    if (!isAuthenticated && to.name !== 'Login') {
        return { name: 'Login', replace: true, force: true }
    }
    // return false
})

export default router